package com.jctrip.hotel.source.service.atour;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;

/**
 * @Author henry
 * @Date 2023/6/3 15:17
 * @Description
 */
public class AtourSignUtils {
    public static String genSign(String signKey, Map<String, String> paramMap) {
        if (paramMap == null || paramMap.isEmpty()) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        paramMap.remove("sign");

        Map<String, String> treeMap = new TreeMap<String, String>(paramMap);
        Iterator<String> iter = treeMap.keySet().iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next();
            if (StringUtils.isBlank(ObjectUtils.toString(treeMap.get(name)))) {
                continue;
            }
            sb.append(name).append(treeMap.get(name));
        }
        return DigestUtils.md5DigestAsHex((sb.toString() + signKey).getBytes());
    }
}
